#include "StdAfx.h"
#include "Satellite2D.h"
#include <MyGIS_Common.h>

Satellite2D::Satellite2D(OsgViewerBase* parentViewer)
{
	_parentViewer = parentViewer;
	EventCenter::inst()->registerHandler(this, SCENE_GROUP, _parentViewer->getStationId());
	EventCenter::inst()->registerHandler(this, NETWORK_GROUP);

	_clipGroup = new ClipNode;
	Vec2d leftTop, rightBottom;
	SceneCenter::inst()->LLToXY(-179.99999, 90, leftTop.x(), leftTop.y());
	SceneCenter::inst()->LLToXY(180, -90, rightBottom.x(), rightBottom.y());
	BoundingBox bb;
	bb.set(leftTop.x(), rightBottom.y(), -1, rightBottom.x(), leftTop.y(), 1);
	_clipGroup->createClipBox(bb);

	parentViewer->getLayerManager()->addToNewLayer(_clipGroup, "Satellites");
}

Satellite2D::~Satellite2D(void)
{
	EventCenter::inst()->unregisterHandler(this, SCENE_GROUP, _parentViewer->getStationId());
}

void Satellite2D::handleSystemEvent( unsigned eventType, int param1, int param2 )
{
	if (eventType != FRAME_BEGIN) { return; }
//	if (g_placetableUpdated == false)
//	{
//		g_placeTable.clear();
//	}
//	if (g_satableUpdated == false)
//	{
//		g_satTable.clear();
//	}
	for (unsigned i = 0; i < _satData.size(); ++i)						// read from network
	{
		SatLocation& loc = *_satData[i].get();
		if (loc.num == -1)
		{
			return;
		}

		if (_satellites.find(loc.num) == _satellites.end())
		{
			Satellite* sat = new Satellite("./resources/Satellites/sat1.ive", loc.num, loc.name);
			_satellites[loc.num] = sat;
			_clipGroup->addChild(sat);
		}

		Satellite* satllite = _satellites[loc.num];
		satllite->updateOrbitLine(loc.lon, loc.lat);
		satllite->flashBeam(loc.flash);
		satllite->updateLabel(loc.lon, loc.lat);

		SATLIGHT light;
		light.num = loc.num;
		light.plot_num = 5;
		light.longitude[0] = 100 * (loc.lon - 10);
		light.latitude[0]  = 100 * (loc.lat + 10);
		light.longitude[1] = 100 * (loc.lon + 10);
		light.latitude[1]  = 100 * (loc.lat + 10);
		light.longitude[2] = 100 * (loc.lon + 10);
		light.latitude[2]  = 100 * (loc.lat - 10);
		light.longitude[3] = 100 * (loc.lon - 10);
		light.latitude[3]  = 100 * (loc.lat - 10);
		light.longitude[4] = 100 * (loc.lon - 5);
		light.latitude[4]  = 100 * (loc.lat - 5);

		satllite->updateBeam(light.plot_num, light.longitude, light.latitude);
	}

	_satData.clear();

//		else if (netData.mesg_head == 400)//SATLIGHT
//		{
//			//continue;
//			SATLIGHT loc;
//			memcpy(&loc,netData.data,sizeof(SATLIGHT));
//
//			/*if (loc.num == 1)
//			{
//				loc.longitude[0] = -160 * 100;
//				loc.longitude[1] = -1 * 100;
//				loc.longitude[2] = 0 * 100;
//				loc.longitude[3] = 179 * 100;
//				loc.latitude[0] = 80 * 100;
//				loc.latitude[1] = 75 * 100;
//				loc.latitude[2] = 75 * 100;
//				loc.latitude[3] = 85 * 100;
//				loc.plot_num = 4;
//			}*/
//			if (loc.plot_num == 0)
//			{
//				continue;
//			}
//			if (_satellites.find(loc.num) != _satellites.end())
//			{
//				_satellites[loc.num]->updateBeam(loc.plot_num, loc.longitude, loc.latitude);
//				/*char buffer[100];
//				FILE *fp=fopen("corrdination.txt","wt");
//				sprintf(buffer,"%d\n",loc.num);
//				fwrite(buffer,1,strlen(buffer),fp);
//				for(int i=0;i<loc.plot_num;i++)
//				{
//					sprintf(buffer,"%d %d\n",loc.longitude[i], loc.latitude[i]);
//					fwrite(buffer,1,strlen(buffer),fp);
//				}
//				sprintf(buffer,"%s\n","************");
//				fwrite(buffer,1,strlen(buffer),fp);
//				fclose(fp);*/
//			}
//		}
//		else if (netData.mesg_head == 200)//LOCATIONTABLE
//		{
//			LOCATIONTABLE loc;
//			memcpy(&loc,netData.data,sizeof(LOCATIONTABLE));
//			g_placeTable.push_back(loc);
//			g_placetableUpdated = true;
//		}
//		else if (netData.mesg_head == 300)//SATTABLE
//		{
//			SATTABLE loc;
//			memcpy(&loc,netData.data,sizeof(SATTABLE));
//			g_satTable.push_back(loc);
//			g_satableUpdated = true; 
//		}
//	}
//	
}

void Satellite2D::handleCustomEvent( unsigned eventType, int param1, int param2 )
{
	if (eventType != DEFAULT_NETWORK_DATA)
	{
		return;
	}

	switch (param1)
	{
	case 100:
		{
			DataCenter::DataPtr<SatLocation> loc;
			DataCenter::inst()->getData(loc, param2);
			static ofstream fout("d:\\1.txt");
			fout << param2 << endl;
			_satData.push_back(loc);
			break;
		}
	}
}
void Satellite2D::followSatellite( int num )
{
	if (_satellites.find(num) == _satellites.end())
	{
		return;
	}

	_parentViewer->getMainCamera()->flyTo(_satellites[num]);
}

